/**
 * @file    MIMXRT1064xxxxA_Project.c
 * @brief   Application entry point.
 */
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MIMXRT1064.h"
#include "fsl_debug_console.h"
#include "fsl_semc.h"
/* TODO: insert other include files here. */
#include "my_sys.h"
#include "lvgl.h"
#include "littlevgl_support.h"
#include "lv_examples/lv_exc_screen/exc_screen.h"
/* TODO: insert other definitions and declarations here. */
uint8_t txbuff[]   = "Lpuart polling example\r\nBoard will send back received characters\r\n";
uint8_t rxbuff[20] = {0};

/*
 * @brief   Application entry point.
 */
int main(void) {

  	/* Init board hardware. */
    BOARD_InitBootPins();
    BOARD_InitLCD();
//    DEMO_InitLcdBackLight();
    BOARD_InitSDRAM();
    BOARD_InitBootClocks();
    BOARD_InitBootPeripherals();
  	/* Init FSL debug console. */
    BOARD_InitDebugConsole();
//    BOARD_InitSEMC();
//    memset((void *)EXT_BUFFER0_ADDR, 0xAAAA, BOARD_LCDIF_PANEL_WIDTH * BOARD_LCDIF_PANEL_HEIGHT *2);
//    DEMO_InitLcd();
//	ELCDIF_RgbModeStart(LCDIF);

    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();
    lvAdd_excScreen();

    PRINTF("Hello World\n");

//    lpuart_transfer_t sendXfer;
//    uint8_t g_tipString[] = "this is the buf";
//    sendXfer.data     = g_tipString;
//    sendXfer.dataSize = sizeof(g_tipString) - 1;
//    LPUART_SendEDMA(LPUART3, &BOARD_LPUART3_LPUART_eDMA_Handle, &sendXfer);

    EXAMPLE_StartRingBufferEDMA();

    GPT_StartTimer(BOARD_GPT1_PERIPHERAL);
//    LPUART_WriteBlocking(LPUART3, txbuff, sizeof(txbuff) - 1);


    uint32_t t_counter=0;
    /* Force the counter to be placed into memory. */
    /* Enter an infinite loop, just incrementing a counter. */
    while(1) {
//        GPIO_PinWrite(GPIO1, 9 , 0);
//        delay();
//        GPIO_PinWrite(GPIO1, 9 , 1);
//        delay();

//    	t_counter = DMA0->TCD[0].BITER_ELINKYES - DMA0->TCD[0].CITER_ELINKYES;

//    	t_counter = BOARD_LPUART3_LPUART_eDMA_Handle.rxDataSizeAll -
//    			((uint32_t)BOARD_LPUART3_LPUART_eDMA_Handle.nbytes *
//    					EDMA_GetRemainingMajorLoopCount(BOARD_LPUART3_LPUART_eDMA_Handle.rxEdmaHandle->base, BOARD_LPUART3_LPUART_eDMA_Handle.rxEdmaHandle->channel));

//    	if(t_counter >=30){
//    		LPUART_TransferAbortReceiveEDMA(LPUART3,  &BOARD_LPUART3_LPUART_eDMA_Handle);
//    		LPUART_ReceiveEDMA(LPUART3,  &BOARD_LPUART3_LPUART_eDMA_Handle, &xferConfig);
//    		EXAMPLE_StartRingBufferEDMA();
//    	}


//        LPUART_ReadBlocking(LPUART3, &ch, 1);
//        LPUART_WriteBlocking(LPUART3, &ch, 1);

    	delay_ms(2);
        lv_tick_inc(2);
        lv_task_handler();


        t_counter++;
        switch (t_counter) {
        case 100:
            break;
        case 200:
            lvChange_pagemaingauge(5070,5040,5590,5000);
            lvChange_pagemainBar(5000, 5000);
            lvChange_pagePromotionGauge(2000);
            break;
        case 300:
            lvChange_pagemaingauge(1050,2020,8040,8000);
            lvChange_pagemainBar(1000, 8000);
            lvChange_pagePromotionGauge(12000);
            break;
        case 400:
            lvChange_pagemaingauge(0,0,0,0);
            lvChange_pagemainBar(0, 0);
            lvChange_pagePromotionGauge(0);
            break;
        case 500:
            t_counter = 0;
            break;
        default:
            break;
        }


        /* 'Dummy' NOP to allow source level single stepping of
            tight while() loop */
        __asm volatile ("nop");
    }
    return 0 ;
}

